package com.by_man.presenter.module_health_records.access_records;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.presenter.module_gestureLock.GestureLockActivity;
import com.by_man.presenter.module_health_consultant.BitmapCache;
import com.by_man.repository.IpAddress;
import com.by_man.repository.PostStringRequest;
import com.by_man.repository.UserInfos;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by man on 2017-03-17.
 */
public class OCRReportOriginalActivity extends AppCompatActivity {
    private RequestQueue rQueue;
    private ImageLoader imageLoader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //rQueue = Volley.newRequestQueue(this);
        rQueue = SharedApplication.getrQueue(getApplication());
        rQueue.start();
        //imageLoader = new ImageLoader(rQueue,new BitmapCache());
        imageLoader = SharedApplication.getImageLoader(getApplication());
        setContentView(R.layout.activity_ocr_report_original);
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, OCRActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, OCRActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode,event);
    }

    protected void onRestart()
    {
        super.onRestart();
        Intent intent=new Intent(this,GestureLockActivity.class);
        intent.putExtra("class",this.getClass().getName());
        intent.putExtra("position", 0);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("原图");
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        Intent intent=getIntent();
        String assay_pic=intent.getStringExtra("picture");
        TextView xiangmu=(TextView)findViewById(R.id.assay_xiangmu);
        xiangmu.setText(assay_pic);
        final ListView lv= (ListView) findViewById(R.id.list_pic_yuantu);
        HashMap<String,String> params = new HashMap<>();
        params.put("token", UserInfos.token);
        params.put("arg",assay_pic);
        PostStringRequest stringRequest = new PostStringRequest(Request.Method.POST,
                IpAddress.OCR_Image, params,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        try {
                            JSONArray jsonArray = new JSONArray(s);
                            ArrayList<String> paths = new ArrayList<>();
                            JSONObject temp;
                            for (int i = 0; i < jsonArray.length(); i++) {
                                temp = (JSONObject) jsonArray.get(i);
                                String path = IpAddress.Load_Image + temp.getString("path");
                                paths.add(path);
                            }
                            ListViewAdapter adapter = new ListViewAdapter(paths);
                            lv.setAdapter(adapter);
                        } catch (JSONException je) {
                            je.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("OCRReportOriginalError",volleyError.getMessage(),volleyError);
                    }
                });
        rQueue.add(stringRequest);
    }

    private class ListViewAdapter extends BaseAdapter {
        ArrayList<String> paths;
        public ListViewAdapter(ArrayList<String> paths) {
            this.paths = paths;
        }

        public int getCount() {
            return paths.size();
        }

        public Object getItem(int position) {
            return paths.get(position);
        }

        public long getItemId(int position) {
            return 0;
        }

        public View getView(int position,View convertView,ViewGroup parent) {
            if (null == convertView) {
                convertView = getLayoutInflater().inflate(R.layout.item_listview_ocr_report_original,parent,false);
            }
            ImageView img = (ImageView) convertView.findViewById(R.id.pic_yuantu);
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(img,R.drawable.no,R.drawable.no);
            imageLoader.get(paths.get(position),listener);
            return convertView;
        }
    }
}
